package com.ysx.member.controller;

import com.ysx.common.repentity.CommonResp;
import com.ysx.member.entity.MemberCodeReq;
import com.ysx.member.entity.MemberLoginReq;
import com.ysx.member.entity.MemberRegister;
import com.ysx.member.repentity.MemberLonginResp;
import com.ysx.member.service.service;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

/**
 * 会员控制器
 * 处理会员相关的HTTP请求，包括注册、登录、验证码发送等功能
 */
@RestController
@RefreshScope
@RequestMapping("/member")
@CrossOrigin
public class MemberController {

    @Value("${test.nacos:default}")
    private String testNacos;

    @Autowired
    Environment environment;


    @Resource
    private service service;



    /**
     * 注册
     * @param memberRegister 会员注册请求参数
     * @return 注册结果响应对象，包含注册成功的会员ID
     */
    @PostMapping("/register")
    public CommonResp<Long> registet(@Valid MemberRegister memberRegister) {
        long register = service.register(memberRegister);

        return new CommonResp<>(register);

    }

    /**
     * 验证码
     * @param memberCodeReq 验证码请求参数
     * @return 验证码发送结果响应对象
     */
    @PostMapping("/send-code")
    public CommonResp<Long> sendCode(@Valid MemberCodeReq memberCodeReq) {
        service.sendCode(memberCodeReq);

        return new CommonResp<>();

    }

    /**
     * 登录
     * @param memberLoginReq 会员登录请求参数
     * @return 登录结果响应对象，包含登录成功后的会员信息
     */
    @PostMapping("/login")
    public CommonResp<MemberLonginResp> login(@Valid MemberLoginReq memberLoginReq) {
        MemberLonginResp resp = service.login(memberLoginReq);

        return new CommonResp<>(resp);

    }
}
